package com.zpark.user_system.servlet;

import com.zpark.user_system.bean.User;
import com.zpark.user_system.utils.JDBCUtils;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;

/**
 * @PackageName: com.zpark.user_system.servlet
 * @ClassName: UserRegServlet
 * @Description:
 * @author: RZS
 * @date: 2023/3/21  15:33
 */
@WebServlet("/user/user_reg.do")
public class UserRegServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 后台获取用户提交的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String[] languages = request.getParameterValues("language");
        String city = request.getParameter("city");

        // 校验数据（使用正则表达式校验）

        // 通过注册名字向数据库查询数据 select username, password, sex, language, city from user where username=?
        // 获取数据库连接对象
        Connection conn = JDBCUtils.getConnection();
        // 编写sql
        String sql = "select username, password, sex, language, city from user where username=?";
        // 创建sql预编译执行对象
        PreparedStatement stat = conn.prepareStatement(sql);

        // 替换占位符
        stat.setString(1, username);

        // 执行sql语句
        ResultSet rest = stat.executeQuery();
        // 将rest结果封装到user对象当中
        User user = null;
        while (rest.next()) {
            // 取出数据
            user = new User();
            user.setUsername(rest.getString("username"));
            user.setPassword(rest.getString("password"));
            user.setSex(rest.getString("sex"));
            user.setLanguage(rest.getString("language"));
            user.setCity(rest.getString("city"));
        }
        // 判断是否有结果
        if (user == null) {
            // 可以注册数据
            String insert = "insert into user(username, password, sex, language, city) values(?, ?, ?, ?, ?)";
            stat = conn.prepareStatement(insert);
            // 替换占位符
            stat.setString(1, username);
            stat.setString(2, password);
            stat.setString(3, sex);
            stat.setString(4, Arrays.toString(languages));
            stat.setString(5, city);

            // 执行sql
            stat.executeUpdate();

            // 重定向到用户登录页面
            response.sendRedirect(request.getContextPath() + "/jsp/login.jsp");
        }else {
            // 提示用户注册失败，跳转到注册失败页面
            request.setAttribute("regError", "用户已存在，注册失败！！！");
            // 转发到用户注册页面
            request.getRequestDispatcher("/jsp/reg.jsp").forward(request, response);
        }

        // 关闭数据库连接
        JDBCUtils.close(conn, stat, null);
    }
}
